package org.dlnu.xeon.controller;

import lombok.extern.slf4j.Slf4j;
import org.dlnu.xeon.controller.Response.Response;
import org.dlnu.xeon.domain.User;
import org.dlnu.xeon.service.UserService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;

import static org.dlnu.xeon.common.consts.StaticData.NUMBER_ZERO;
import static org.dlnu.xeon.common.enums.StatusCode.*;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @PostMapping("/loginUser")
    public Response selectLoginUser(@RequestBody User user){
        if (StringUtils.isEmpty(user.getNumber())) {
            log.error("[Boot2022|UserController|insertUser] the parameter username is error. username = " + user.getNumber());
            return new Response(PARAMETER_VALIDATE_ILLEGAL, user.getNumber(), "username parameter error");
        }
        if (StringUtils.isEmpty(user.getPassword())) {
            log.error("[Boot2022|UserController|insertUser] the parameter password is error. password = " + user.getPassword());
            return new Response(PARAMETER_VALIDATE_ILLEGAL, user.getPassword(), "password parameter error");
        }
        User user1 = userService.selectLoginUser(user.getNumber(),user.getPassword());
        return new Response(SERVICE_RUN_SUCCESS, user1, "Service running successfully");
    }

    @PostMapping("/selectAllUser")
    public Response selectAllUser(){
        List<User> users = userService.selectAllUser();
        if (null == users) {
            log.error("[Boot2022|UserController|selectUser] the result is null！");
            return new Response(DATA_NOT_EXIST, null, "the result--user is null");
        }
        return new Response(SERVICE_RUN_SUCCESS, users, "Service running successfully");
    }

    @PostMapping("/getUserMsgByUid")
    public Response selectUser(@RequestBody User user) {
        if (null == user.getUid() || NUMBER_ZERO > user.getUid()) {
            log.error("[Boot2022|UserController|selectUser] the parameter uid is error. uid = " + user.getUid());
            return new Response(PARAMETER_VALIDATE_ILLEGAL, user.getUid(), "uid parameter error");
        }
        User resuser = userService.selectUser(user.getUid());
        if (null == resuser) {
            log.error("[Boot2022|UserController|selectUser] the result is null！");
            return new Response(DATA_NOT_EXIST, null, "the result--user is null");
        }
        return new Response(SERVICE_RUN_SUCCESS, resuser, "Service running successfully");
    }

    @PostMapping("/registerUser")
    public Response insertUser(@RequestBody User user) {
        if (StringUtils.isEmpty(user.getNumber())) {
            log.error("[Boot2022|UserController|insertUser] the parameter username is error. username = " + user.getNumber());
            return new Response(PARAMETER_VALIDATE_ILLEGAL, user.getNumber(), "username parameter error");
        }
        if (StringUtils.isEmpty(user.getPassword())) {
            log.error("[Boot2022|UserController|insertUser] the parameter password is error. password = " + user.getPassword());
            return new Response(PARAMETER_VALIDATE_ILLEGAL, user.getPassword(), "password parameter error");
        }
        Long uid = userService.insertUser(user);
        if (null == uid) {
            log.error("[Boot2022|UserController|insertUser] the result is null！");
            return new Response(DATA_NOT_EXIST, null, "the result--uid is null");
        }
        return new Response(SERVICE_RUN_SUCCESS, uid, "Service running successfully");
    }

    @PostMapping("/insertUserAndAdmin")
    public Response insertUserAndAdmin(@RequestBody User user) {
        if (StringUtils.isEmpty(user.getNumber())) {
            log.error("[Boot2022|UserController|insertUser] the parameter username is error. username = " + user.getNumber());
            return new Response(PARAMETER_VALIDATE_ILLEGAL, user.getNumber(), "username parameter error");
        }
        if (StringUtils.isEmpty(user.getPassword())) {
            log.error("[Boot2022|UserController|insertUser] the parameter password is error. password = " + user.getPassword());
            return new Response(PARAMETER_VALIDATE_ILLEGAL, user.getPassword(), "password parameter error");
        }
        Long uid = userService.insertUserAndAdmin(user);
        if (null == uid) {
            log.error("[Boot2022|UserController|insertUser] the result is null！");
            return new Response(DATA_NOT_EXIST, null, "the result--uid is null");
        }
        return new Response(SERVICE_RUN_SUCCESS, uid, "Service running successfully");
    }


    @PostMapping("/updateUser")
    public Response updateUser(@RequestBody User user) {
        if (null == user.getUid() || NUMBER_ZERO > user.getUid()) {
            log.error("[Boot2022|UserController|updateUser] the parameter uid is error. uid = " + user.getUid());
            return new Response(PARAMETER_VALIDATE_ILLEGAL, user.getUid(), "uid parameter error");
        }
        User resUser = userService.updateUser(user);
        if (null == resUser) {
            log.error("[Boot2022|UserController|updateUser] the result is null！");
            return new Response(DATA_NOT_EXIST, null, "the result--resUser is null");
        }
        return new Response(SERVICE_RUN_SUCCESS, resUser, "Service running successfully");
    }

    @PostMapping("/deleteUser")
    public Response deleteUser(@RequestBody User user) {
        if (null == user.getUid() || NUMBER_ZERO > user.getUid()) {
            log.error("[Boot2022|UserController|deleteUser] the parameter uid is error. uid = " + user.getUid());
            return new Response(PARAMETER_VALIDATE_ILLEGAL, user.getUid(), "uid parameter error");
        }
        userService.deleteUser(user.getUid());
        return new Response(SERVICE_RUN_SUCCESS, null, "Service running successfully");
    }
}

